package com.thinkgsp.webapp.controller.base;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ch.qos.logback.classic.Logger;

import com.thinkgsp.jxc.model.Customer;
import com.thinkgsp.jxc.persistence.mybatis.PagingResult;
import com.thinkgsp.jxc.service.CustomerService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExportExcel;
import com.thinkgsp.webapp.util.ExtReturn;

@Controller
@RequestMapping(value = "/base")
public class CustomerController  extends BaseController{
	@Autowired
	private CustomerService customerServiceImpl;
	
	@RequestMapping("/custlist.do")
    @ResponseBody
    public Object  getAllCust(HttpServletRequest request,
			HttpServletResponse response){
		try {
			Integer custType =3;
			List<Customer> custs = customerServiceImpl.getAllCust(custType);
			return custs;
		} catch (Exception e) {
			return new ExtReturn(false,"查询客户资料出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping("/searchcust.do")
    @ResponseBody
    public Object searchCust(HttpServletRequest request,
			HttpServletResponse response){
		try {
			String val=request.getParameter("val");
			if(StringUtils.isBlank(val)) return new ExtReturn(false,"条件不能为空");
			int size = Integer.parseInt(request.getParameter("limit"));
			int start = Integer.parseInt(request.getParameter("start"));
			Integer custType =3;
			PagingResult<Customer> custs=null;
			if(val.equals("all")){
				custs= customerServiceImpl.getAllCust(custType, start, size);
			}else{
				custs=this.customerServiceImpl.searchCust(custType, val, start, size);
			}
			return custs;
		} catch (Exception e) {
			return new ExtReturn(false,"查询客户资料出错:"+e.getMessage(),e);
		}
		
	}
	@RequestMapping(value = "/updcustomer.do",method = RequestMethod.POST )
	@ResponseBody
	public Object updCustomer(Model model ,@RequestBody Customer cust) {
		try {
			this.customerServiceImpl.saveCust(cust);
			this.loger.debug("updated customer");
			return new ExtReturn(true,"客户修改成功");
		} catch (Exception e) {
			this.loger.error(e);
			return new ExtReturn(false,"客户资料修改出错:"+e.getMessage(),e);
		}	
	}
	
	@RequestMapping(value = "/getCustomerState.do")
	@ResponseBody
	public Object getCustomerState(@RequestParam Integer custId) {
		try {
			return this.customerServiceImpl.checkCustomerState(custId);
		} catch (Exception e) {
			this.loger.error(e);
			return new ExtReturn(false,"获取客户状态出错:"+e.getMessage(),e);
		}	
	}
	@RequestMapping(value = "/delCust.do")
	@ResponseBody
	public Object del(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String pam=request.getParameter("ids");
			String[] ids=pam.split(",");
			for (String id : ids) {
				this.customerServiceImpl.deleteCust(Integer.decode(id));
			}
			return new ExtReturn(true,"删除客户信息成功");
		} catch (Exception e) {
			this.loger.error(e);
			return new ExtReturn(false,"删除客户信息出错:"+e.getMessage(),e);
		}
	}
	
	@RequestMapping(value = "/restoreCust.do")
	@ResponseBody
	public Object restoreCust(@RequestParam Integer custId) {
		try {
			this.customerServiceImpl.restoreCust(custId);
			return new ExtReturn(true,"客户信息还原成功");
		} catch (Exception e) {
			this.loger.error(e);
			return new ExtReturn(false,"客户信息还原出错:"+e.getMessage(),e);
		}	
	}
	
	@RequestMapping("/recycleCust.do")
    @ResponseBody
    public Object recycleCust(HttpServletRequest request,
			HttpServletResponse response){
		try {
			return this.customerServiceImpl.getDeletedCustomers();
		} catch (Exception e) {
			return new ExtReturn(false,"查询客户资料出错:"+e.getMessage(),e);
		}
		
	}
	
	@RequestMapping("/getCustomerXLS.do")
	public void getCustomerXLS(HttpServletRequest request,HttpServletResponse response) throws IOException{
		try{
			response.setContentType("octets/stream");
	        response.addHeader("Content-Disposition","attachment;filename="+ new String(("客户资料导出").getBytes("GB2312"),"iso8859-1")+".xls");
	        OutputStream out = response.getOutputStream();
	        ExportExcel<Customer> ex=new ExportExcel<Customer>();
	        List<Customer> cust=customerServiceImpl.getAllCust(3);
	        HSSFWorkbook book=ex.exportExcelByFieldAnnotation(cust);
	        book.write(out);
		}catch(Exception ex){
			loger.debug(ex);
		}
	}
	
}
